﻿using System;
using System.Collections.Generic;
using Gp.Scripts.Core;
using Gp.Scripts.Data;
using Gp.Scripts.Data.Skills;
using UnityEngine;

namespace Gp.Scripts.Equip {
    public class WeaponAmmoChangeEvent {
        public readonly WeaponEntity Entity;

        public WeaponAmmoChangeEvent(WeaponEntity entity) {
            Entity = entity;
        }
    }


    public interface IReloadable {
        void Reload();
        IEnumerable<ResourceCost> ReloadCost();
        bool CanReload();
    }


    [Serializable]
    public class RangedWeaponInfo : WeaponTypeInfo<RangedBaseWeaponType>, IReloadable {
        public int SelectingAmmo => Math.Min(ammoRemain, ShootSpeed);

        // 剩余弹药
        [SerializeField]
        private int ammoRemain;

        public int AmmoRemain {
            get => ammoRemain;
            set {
                ammoRemain = Math.Clamp(value, 0, maxAmmo);
                MainArchitecture.Interface.SendEvent(new WeaponAmmoChangeEvent(Entity));
            }
        }

        public int maxAmmo;


        public override bool IsBroken { get; set; }


        public RangedWeaponInfo(RangedBaseWeaponType type, WeaponEntity weaponEntity) : base(type, weaponEntity) {
            maxAmmo = type.capacity;
            ammoRemain = maxAmmo;
        }

        public override IEnumerable<SkillTags> SkillTag() {
            yield return SkillTags.RangedAttack;
        }


        public override int GetDamage() {
            return SelectingAmmo * WeaponType.damage;
        }


        public override void AfterAttack(BaseUnit target, AttackActionResult res) {
            AfterAttack(res);
        }


        public void AfterAttack(AttackActionResult res = null) {
            // // 穿甲标签不会增长压制点
            // if (res == null || !res.Request.Tags.Contains(SkillTags.ArmorPiercing)) {
            //     // 增加压制点
            //     CellGridSystem.Instance.CellGridModel.Container.Add(Unit, GetSp());
            // }
            //
            
            // 减少弹药
            AmmoRemain -= SelectingAmmo;
        }
        
        public override bool CanAttack() {
            return SelectingAmmo > 0 && ammoRemain > 0;
        }


        public bool HasAmmo => ammoRemain > 0;
        



        public virtual bool CanCounterAttack() {
            return false;
        }
        
        
        

        // public override int ActionCost => ShootSpeed == 1 ? 1 : 2;


        public override IEnumerable<WeaponAbilityEntity> WeaponAbilities() {
            // yield return WeaponAbilityEntity.Build<ReloadWeaponAbilityConfig>(this.Entity);
            yield break;
        }
        
        
        public IEnumerable<ResourceCost> ReloadCost() {
            return this.WeaponType.Cost.ToCosts();
        }
        
        public void Reload() {
            AmmoRemain = maxAmmo;
            MainArchitecture.Interface.SendEvent(new WeaponAmmoChangeEvent(Entity));
        }

        public bool CanReload() {
            return ammoRemain < maxAmmo;
        }


        


        private int GetSp() {
            return 0;
        }




        public bool CanDcShoot => WeaponType.Discontinuous;  
        public bool DcShoot { get; private set; }

        public int ShootSpeed => CanDcShoot && DcShoot ? 1 : WeaponType.shootSpeed;
        
        public void SetShootMode(bool dcShoot) {
            DcShoot = dcShoot && CanDcShoot;
        }
    }
}